package com.etbsms.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.etbsms.dao.IAgricultureAndFoodDAO;
import com.etbsms.domain.AgricultureAndFood;
import com.etbsms.domain.Users;
import com.etbsms.form.PageForm;
import com.etbsms.form.QueryAAFForm;
import com.etbsms.service.IAgricultureAndFoodService;
import com.etbsms.util.Constants;
import com.etbsms.util.Constants.FLOW_TYPE;

@Service
@Component("agricultureAndFoodService")
public class AgricultureAndFoodServiceImpl implements
		IAgricultureAndFoodService {
	
	@Autowired
	private IAgricultureAndFoodDAO agricultureAndFoodDAO;

	@Override
	public boolean save(AgricultureAndFood entity) {
		// TODO Auto-generated method stub
//		entity.setClazz(Utils.transClazz2Code(entity.getClazz()));
		entity.setCreated(new Date());
		entity.setUpdated(null);
		entity.setLiterature_desc(entity.getLiterature() != 4 ? null : entity.getLiterature_desc());		
		return agricultureAndFoodDAO.save(entity);
	}

	@Override
	public boolean update(AgricultureAndFood entity) {
		// TODO Auto-generated method stub
//		entity.setClazz(Utils.transClazz2Code(entity.getClazz()));		
		entity.setLiterature_desc(entity.getLiterature() != 4 ? null : entity.getLiterature_desc());
		return agricultureAndFoodDAO.update(entity);
	}

	@Override
	public PageForm<AgricultureAndFood> getOwnForms(Users users,PageForm<AgricultureAndFood> pageForm,boolean isAnalyzed) {
		// TODO Auto-generated method stub
		return agricultureAndFoodDAO.getOwnForms(users,pageForm,isAnalyzed);
	}

	@Override
	public AgricultureAndFood get(int seq_no) {
		// TODO Auto-generated method stub
		AgricultureAndFood entity = agricultureAndFoodDAO.get(seq_no);
//		entity.setClazz(Utils.transCode2Clazz(entity.getClazz()));
		return entity;
	}

	/**比對重覆**/
	@Override
	public String comparison(String title_o, String author) {
		String rec_no = null;		
		List<AgricultureAndFood> list = agricultureAndFoodDAO.getAll(Constants.FLOW_KENI,FLOW_TYPE.ge);		
		
		for(AgricultureAndFood entity : list){
			String _title_o = replacement(entity.getTitle_o());
			String _author  = replacement(entity.getAuthor());			
			String _title_o_1 = replacement(title_o);
			String _author_1  = replacement(author);	
			if(_title_o_1.equals(_title_o) && _author_1.equals(_author)){
				rec_no = entity.getRec_no();
				break;
			}
		}
		return rec_no;
	}
	
	@Override
	public AgricultureAndFood get(String rec_no){
		AgricultureAndFood entity = agricultureAndFoodDAO.get(rec_no);
//		entity.setClazz(Utils.transCode2Clazz(entity.getClazz()));
		return entity;
	}

	
	private static String replacement(String str){
		String symbol = "[/,\\.\\s~!@#$%^&*\\(\\)_\\-';]";		
		str = str.toLowerCase()
				.replaceAll(symbol, "");		
		return str;
	}

	@Override
	public QueryAAFForm query(QueryAAFForm form,FLOW_TYPE type) {
		// TODO Auto-generated method stub
		return agricultureAndFoodDAO.query(form,type);
	}

	@Override
	public List<AgricultureAndFood> getAll(int flow,Constants.FLOW_TYPE type) {
		// TODO Auto-generated method stub
		return agricultureAndFoodDAO.getAll(flow,type);
	}	
	
	@Override
	public QueryAAFForm recNo_query(QueryAAFForm form, FLOW_TYPE type){
		return agricultureAndFoodDAO.recNo_query(form,type);
	}

	@Override
	public List<Users> getInputUsers() {
		// TODO Auto-generated method stub
		return agricultureAndFoodDAO.getInputUsers();
	}

	@Override
	public void changeTemporary(Users users) {
		// TODO Auto-generated method stub
		agricultureAndFoodDAO.changeTemporary(users);
	}
}
